import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '@/components/Home'
import Love from '@/components/love'
import Users from '@/components/Users'
import Categories from '@/components/Categories'
import Goods from '@/components/Goods'
import Orders from '@/components/Orders'
import Params from '@/components/Params'
import Roles from '@/components/Roles'
import Rights from '@/components/Rights'
import Reports from '@/components/Reports'
import Add from '@/components/Add'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/love',
      children:
      [
        {
          path: '/love',
          component: Love
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/categories',
          component: Categories
        },
        {
          path: '/orders',
          component: Orders
        },
        {
          path: '/reports',
          component: Reports
        },
        {
          path: '/goods/add',
          component: Add
        }
      ]
    }
  ]
})

// 路由导航首位
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    next('/login')
  } else {
    next()
  }
})
export default router
